package com.ccjq.shosestore.member.dao.impl;

import com.ccjq.shosestore.core.dbmanager.DbConnection;
import com.ccjq.shosestore.member.dao.UserDao;
import com.ccjq.shosestore.member.po.UserModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private  Connection conn = null;
    public int save(UserModel model) {
        int  result = 0;
        String sql="insert into t_user values(?,?,?,?)";
        conn = DbConnection.getConnection();
        PreparedStatement st=null;
        try {
            st=conn.prepareStatement(sql);
            st.setString(1,model.getUserid());
            st.setString(2,model.getUsername());
            st.setString(3,model.getPassword());
            st.setString(4,model.getMobile_phone());
            result = st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(null,st,conn);
        }
        return result;
    }

    public int update(UserModel model) {
        int  result = 0;
        String sql="update t_user set username=?,password=?,mobel_phone where user_id=?";
        conn = DbConnection.getConnection();
        PreparedStatement st=null;
        try {
            st=conn.prepareStatement(sql);
            st.setString(4,model.getUserid());
            st.setString(1,model.getUsername());
            st.setString(2,model.getPassword());
            st.setString(3,model.getMobile_phone());
            result = st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(null,st,conn);
        }
        return result;
    }

    public int delete(String userid) {
        int  result = 0;
        String sql="delete from t_user where user_id=?";
        conn = DbConnection.getConnection();
        PreparedStatement st=null;
        try {
            st=conn.prepareStatement(sql);
            st.setString(1,userid);
            result = st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(null,st,conn);
        }
        return result;
    }
    public UserModel findUserByName(String username) {
        UserModel userModel=null;
        String sql="select user_id,username,password,mobel_phone from t_user where username=?";
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            conn = DbConnection.getConnection();
            st=conn.prepareStatement(sql);
            st.setString(1,username);
            rs=st.executeQuery();
            if (rs.next())
            {
                userModel = new UserModel();
                userModel.setUserid(rs.getString(1));
                userModel.setUsername(rs.getString(2));
                userModel.setPassword(rs.getString(3));
                userModel.setMobile_phone(rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(rs,st,conn);
        }
        return userModel;
    }

    public UserModel findUserByNamePw(String username, String password) {
        UserModel userModel=null;
        String sql="select user_id,username,password,mobel_phone from t_user where username=? and password=?";
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            conn = DbConnection.getConnection();
            st=conn.prepareStatement(sql);
            st.setString(1,username);
            st.setString(2,password);
            rs=st.executeQuery();
            if (rs.next())
            {
                userModel = new UserModel();
                userModel.setUserid(rs.getString(1));
                userModel.setUsername(rs.getString(2));
                userModel.setPassword(rs.getString(3));
                userModel.setMobile_phone(rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(rs,st,conn);
        }
        return userModel;
    }
    public UserModel findUserById(String userId) {
        UserModel userModel=null;
        String sql="select user_id,username,password,mobel_phone from t_user where user_id=?";
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            conn = DbConnection.getConnection();
            st=conn.prepareStatement(sql);
            st.setString(1,userId);
            rs=st.executeQuery();
            if (rs.next())
            {
                userModel = new UserModel();
                userModel.setUserid(rs.getString(1));
                userModel.setUsername(rs.getString(2));
                userModel.setPassword(rs.getString(3));
                userModel.setMobile_phone(rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(rs,st,conn);
        }
        return userModel;
    }


}
